package com.example.fenye.controller.forum;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.example.fenye.entity.forum.ForumUser;
import com.example.fenye.service.forum.ForumUserService;
import com.example.fenye.util.CharacterUtils;
import com.example.fenye.util.MD5Utils;

@Controller
@RequestMapping(value = "/forum/user")
public class ForumUserController {
	@Resource
	ForumUserService userService;
	
	@ResponseBody
    @RequestMapping(value = "/register", produces = {"application/json;charset=UTF-8"})
    public ForumUser register(ForumUser user){
		String salt =CharacterUtils.getRandomString(8);
        user.setUserSalt(salt);
		user.setUserPass(MD5Utils.MD5Encode(user.getUserPass()+salt,"utf-8"));
		
		
		user.setUserDate(new Date());
		Long userId = userService.addUser(user);
        user.setUserId(userId);
        return user;
    }
	
	@GetMapping(value = "/toRegister")
    public ModelAndView toRegister(){
        ModelAndView mav = new ModelAndView("forum/register");
        return mav;
    }
}
